package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetMealService setMealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
    public Result setMeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.insertMeal(setmealDTO);
        return Result.success();



    }
    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public Result getSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO){
       PageResult pageResult =  setMealService.getSetmealPage(setmealPageQueryDTO);
       return Result.success(pageResult);

    }

    /**
     *套餐批量删除 @RequestParam 由于是一个集合类型需要识别
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCatch",allEntries = true)
    public Result deleteBySetmealIds(@RequestParam List<Long> ids){
        setMealService.deleteBySetmealIds(ids);
        return Result.success();
    }

    /**
     * 套餐回显 数据直接写在路径上
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getBySetmealId(@PathVariable Long id){
     SetmealVO setmealVO = setMealService.getBySetmealId(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "updateSetmealWithDish",allEntries = true)
    public Result updateSetmealWithDish(@RequestBody SetmealDTO setmealDTO){
        setMealService.updateSetmealWithDish(setmealDTO);
     return Result.success();
    }

    /**
     * 启售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "StartOrStop",allEntries = true)
    public Result StartOrStop(@PathVariable Integer status,Long id){
        setMealService.StartOrStop(status,id);
        return Result.success();
    }




}
